package com.xxrl.shop.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.domain.Page;

/**
 * @author mis
 */
public class PageTool {

    private PageTool() {

    }
    static {
        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
        JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask();
    }

    public static <T> String pageToJson(Page<T> page) {
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(page.getContent());
        JSONObject json = new JSONObject();
        json.put("total",page.getTotalElements());
        json.put("items",jsonArray);
        return json.toString();
    }
}
